package example;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import my.com.sunway.bean.*;
import my.com.sunway.util.*;
import java.text.*;
import java.util.*;
import org.apache.log4j.*;

/**
 * @author Deepak Kumar
 *
 * http://www.roseindia.net
 * Hibernate example to inset data into Contact table
 */
public class SelectF0911 {
 static Logger logger = Logger.getLogger(InsertExample.class);

 public static void main(String[] args) {
  Session session = null;
  String queryName = "";
  Query query = null;
  int max_row = 10;
  
  try{
   // This step will read hibernate.cfg.xml and prepare hibernate for use
   session = HibernateUtil.currentSession();
   SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd");
   java.util.Date date = new java.util.Date();
   
   if(args.length>=2){
    logger.info("queryName = "+args[0]);
    logger.info("paramValue = "+args[1]);
   
    if(args[0].matches("my\\.com\\.sunway\\.query\\.F0911\\.getQueryBy[a-zA-Z]+")){
     queryName = args[0];
     logger.info("queryName = "+args[0]);
    }else{
     //default
     logger.debug("queryName = <default>");
     queryName = "my.com.sunway.query.F0911.getQueryByGLDGJ";
    }
    
    query = session.getNamedQuery(queryName);
    
    if(args[1]!=null)
    if(args[1].matches("[0-9]{4}/[0-9]{2}/[0-9]{2}")){
     logger.debug("paramValue = <date>");
     date = sdf1.parse(args[1]);
     SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyDDD");
     String formatted_date = sdf2.format(date);
     long parse_date = Long.parseLong(formatted_date)-1900001;

     logger.info("formatted date = "+formatted_date);
     logger.info("day-of-year = "+parse_date);
     query.setLong("param",parse_date);
    }else if(args[1].matches("[0-9]+\\.[0-9]+")){
     logger.debug("paramValue = <double>");
     query.setDouble("param",Double.parseDouble(args[1]));
    }else if(args[1].matches("[0-9]+")){
     logger.debug("paramValue = <long>");
     query.setLong("param",Long.parseLong(args[1]));
    }else if(args[1].matches("[a-zA-Z]+")){
     logger.debug("paramValue = <string>");
     query.setString("param",args[1]);
    }else{
     logger.debug("paramValue = <default>");
     query.setString("param",args[1]);
    }
    else
     logger.debug("paramValue = <null>");
   }
   
   if(args.length>=3){
    if(args[2].matches("[0-9]{1,4}")){
     max_row = Integer.parseInt(args[2]);
    }
   }
   //query.setFirstResult(1);
   //query.setMaxResults(10);
   ScrollableResults results = query.scroll();
   displayProductsList(results,max_row);
   logger.info("Done");
  }catch(Exception e){
   logger.error(e.toString());
  }finally{
   if(session!=null){
   try{
    // Actual contact insertion will happen at this step
    session.flush();
    session.close();
   }catch(Exception ex){
    logger.error("[error] "+ex);
   }
   }
  }
 }
 
 static public void displayProductsList(ScrollableResults results, int max_row){
  SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyDDD");
  SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd");
 
  try{
  if (results==null){
   logger.info("No account ledger to display.");
   return;
  }
  
  boolean hasNext = true;
  
  for(int row=0; row<max_row;  row++) {
   hasNext = results.next();
   //logger.info("has next? "+hasNext);
   //logger.info("row number? "+results.getRowNumber());
   
   Object obj = results.get(0);
   
   if(obj instanceof F0911){
    F0911 accountLedger = (F0911) obj;
    String msg = "";
    msg += "[no="+(row+1);
    msg += "|doc type="+accountLedger.getGldct();
    msg += "|date="+sdf2.format(sdf1.parse(""+(accountLedger.getGldgj()+1900000)));
    msg += "|doc no="+accountLedger.getGldoc();
    msg += "|extl="+accountLedger.getGlextl();
    msg += "|line="+accountLedger.getGljeln();
    msg += "|line type="+accountLedger.getGllt()+"]";
    logger.info(msg);
    //logger.info("["+row+"] "+accountLedger);
   }else{
    hasNext = false;
   }
  }
  }catch(Exception ex){
   logger.error(ex.toString());
  }
 }
}
